<!--
  * 客户信息表
  *
  * @Author:    dragon
  * @Date:      2023-12-11 13:27:12
  * @Copyright  dragon
-->
<template>
  <a-drawer
      :title="form.id ? '编辑' : '添加'"
      width="500"
      :visible="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }">
      <a-row>
        <a-form-item label="学生姓名" style="width: 100%" name="studentName">
          <a-input style="width: 100%" v-model:value="form.studentName" placeholder="学生姓名"/>
        </a-form-item>
      </a-row>
      <a-row>
        <a-form-item label="免费票数" style="width: 100%" name="freeNum">
          <a-input-number style="width: 100%" v-model:value="form.freeNum" placeholder="免费票数"/>
        </a-form-item>
      </a-row>
      <a-row>
        <a-form-item label="限购票数" style="width: 100%" name="totalNum">
          <a-input-number style="width: 100%" v-model:value="form.totalNum" placeholder="限购票数"/>
        </a-form-item>
      </a-row>
      <a-row>
        <a-form-item label="表演曲目" style="width: 100%" name="performanceTypeIds">
          <PerformanceTypeSelect v-model:value="form.performanceTypeIds" style="width: 100%"/>
        </a-form-item>
      </a-row>

    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-drawer>
</template>
<script setup>
import {reactive, ref, nextTick} from 'vue';
import _ from 'lodash';
import {message} from 'ant-design-vue';
import {SmartLoading} from '/@/components/framework/smart-loading';
import {rolandCustomerApi} from '/@/api/business/roland-customer/roland-customer-api';
import {smartSentry} from '/@/lib/smart-sentry';
import PerformanceTypeSelect from "/@/components/business/roland/performance-type-multiple-select/index.vue";

// ------------------------ 事件 ------------------------

const emits = defineEmits(['reloadList']);

// ------------------------ 显示与隐藏 ------------------------
// 是否显示
const visibleFlag = ref(false);

function show(rowData) {
  console.log(rowData)
  Object.assign(form, formDefault);
  if (rowData && !_.isEmpty(rowData)) {
    Object.assign(form, rowData);
  }
  visibleFlag.value = true;
  nextTick(() => {
    formRef.value.clearValidate();
  });
}

function onClose() {
  Object.assign(form, formDefault);
  visibleFlag.value = false;
}

// ------------------------ 表单 ------------------------

// 组件ref
const formRef = ref();

const formDefault = {
  id: undefined, //id
  studentName: undefined, //学生姓名
  freeNum: 0, //免费票数
  totalNum: 0, //限购票数
  performanceTypeIds: undefined, //表演曲目
};

let form = reactive({...formDefault});

const rules = {
  studentName: [{required: true, message: '学生姓名 必填'}],
};

// 点击确定，验证表单
async function onSubmit() {
  try {
    await formRef.value.validateFields();
    save();
  } catch (err) {
    message.error('参数验证错误，请仔细填写表单数据!');
  }
}

// 新建、编辑API
async function save() {
  SmartLoading.show();
  try {
    if (form.id) {
      await rolandCustomerApi.update(form);
    } else {
      await rolandCustomerApi.add(form);
    }
    message.success('操作成功');
    emits('reloadList');
    onClose();
  } catch (err) {
    smartSentry.captureError(err);
  } finally {
    SmartLoading.hide();
  }
}

defineExpose({
  show,
});
</script>
